package com.zx.dsptuneup_TriMic.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.base.tools.tools.StringUtils;
import com.xujiaji.happybubble.BubbleDialog;
import com.zx.dsptuneup_TriMic.NodeTree.MTreeRecycleAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.ChildDeviceBean;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;
import com.zx.dsptuneup_TriMic.widget.ui.CustomDevMoreDialog;
import com.zx.dsptuneup_TriMic.widget.ui.CustomOperateDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EquipListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private boolean isShow = false;

    private CustomDevMoreDialog mBubbleDialog = null;

    //  左滑操作使用
    private List<List<ChildDeviceBean>>  childs = new ArrayList<>();
    private List<DeviceBean> gridViewBeanList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    List<String> list = new ArrayList<>();

    public List<DeviceBean> getGridViewBeanList() {
        return gridViewBeanList;
    }

    private EquipListAdapter.OnDevicesOperationListener onDevicesOperationListener;

    public void setOnDevicesOperationListener(OnDevicesOperationListener onDevicesOperationListener) {
        this.onDevicesOperationListener = onDevicesOperationListener;
    }

    public EquipListAdapter(Context context, List<DeviceBean> groups, List<List<ChildDeviceBean>> childs){
        this.context=context;
        this.gridViewBeanList=groups;
        this.childs=childs;
    }

    @Override
    public int getGroupCount() {
        return gridViewBeanList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childs.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return gridViewBeanList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childs.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    public void setChilds(List<List<ChildDeviceBean>> childs) {
        this.childs = childs;
        notifyDataSetChanged();
    }

    @Override
    /**
     *
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded 该组是展开状态还是伸缩状态，true=展开
     * @param convertView 重用已有的视图对象
     * @param parent 返回的视图对象始终依附于的视图组
     */
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.parrent_item_list_equip,parent,false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }

        DeviceBean deviceBean = gridViewBeanList.get(groupPosition);

        String devicesInfo = StringUtils.isEmpty(deviceBean.getDeviceName()) ? "未知设备名" : deviceBean.getDeviceName();
        groupViewHolder.main_title.setText(devicesInfo);
        groupViewHolder.tab_tag.setText((groupPosition+1)+".");
        groupViewHolder.sub_title.setText(StringUtils.isEmpty(deviceBean.getPositionName()) ? "未知位置" : deviceBean.getPositionName());

        groupViewHolder.group_title.setText(" (编组:"+ deviceBean.getGroup() + ")");

        // status 1 正常 2 忙碌 3 异常 4 掉线
        GradientDrawable gd = (GradientDrawable)groupViewHolder.ststus_circle.getBackground();
        switch (deviceBean.getStatus()){
            case 1:
                groupViewHolder.equip_status.setText("已上线");
                gd.setColor(Color.parseColor("#5BCB00"));
                break;
            case 2:
                groupViewHolder.equip_status.setText("忙碌");
                gd.setColor(Color.parseColor("#486FFF"));
                break;
            case 3:
                groupViewHolder.equip_status.setText("异常");
                gd.setColor(Color.parseColor("#FEAA00"));
                break;
            case 4:
                groupViewHolder.equip_status.setText("掉线");
                gd.setColor(Color.parseColor("#D2D2D2"));
                break;
        }

        if (isShow){
            groupViewHolder.dev_rl.setVisibility(View.GONE);
            groupViewHolder.more_img.setVisibility(View.GONE);
            groupViewHolder.checkbox_rl.setVisibility(View.VISIBLE);
        }else {
            groupViewHolder.checkbox_rl.setVisibility(View.GONE);
            groupViewHolder.more_img.setVisibility(View.VISIBLE);
            groupViewHolder.dev_rl.setVisibility(View.VISIBLE);
        }

        groupViewHolder.sel_check.setSelected(isItemChecked(groupPosition));

        // 更多点击事件
        groupViewHolder.more_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBubbleDialog == null) {
                    mBubbleDialog =  new CustomDevMoreDialog(context);
                }

                mBubbleDialog.setClickedView(view);
                mBubbleDialog.show();
                mBubbleDialog.setIsParent(true);
                mBubbleDialog.setClickListener(new CustomDevMoreDialog.OnClickCustomButtonListener() {
                    @Override
                    public void onClick(String str, int index) {
                        if(onDevicesOperationListener != null){
                            onDevicesOperationListener.onItemClicked(true,index,groupPosition,-1,gridViewBeanList.get(groupPosition).getId());
                        }
                        mBubbleDialog.dismiss();
                    }
                });
            }
        });

        //如果是展开状态，
        if (isExpanded){
            groupViewHolder.expand_img.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.expand_on));
        }else{
            groupViewHolder.expand_img.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.expand_off));
        }
        return convertView;
    }

    /**
     * group view的点击事件处理
     * */
    public void groupItemClick(int groupPosition) {

        if (isItemChecked(groupPosition)) {
            setItemChecked(groupPosition, false);
        } else {
            setItemChecked(groupPosition, true);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item_list_equip,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        ChildDeviceBean childDev = childs.get(groupPosition).get(childPosition);

        /**
         * 2：吊麦，3：桌麦
         * 吊麦不再可控
         * */
        switch (childDev.getType()){
            case 2:
                childViewHolder.main_title.setText("吊麦");
                childViewHolder.child_more_img.setVisibility(View.GONE);
                break;
            case 3:
                childViewHolder.main_title.setText("桌麦");
                break;
        }

//        childViewHolder.main_title.setText(StringUtils.isEmpty(childDev.getDeviceName()) ? "未知设备名" : childDev.getDeviceName());
        childViewHolder.sub_title.setVisibility(View.GONE);
        childViewHolder.position_img.setVisibility(View.GONE);

        // status 1 正常 2 忙碌 3 异常 4 掉线
        GradientDrawable gd = (GradientDrawable)childViewHolder.child_ststus_circle.getBackground();
        switch (childDev.getState()){
            case 1:
                childViewHolder.child_equip_status.setText("已上线");
                gd.setColor(Color.parseColor("#5BCB00"));
                break;
            case 2:
                childViewHolder.child_equip_status.setText("忙碌");
                gd.setColor(Color.parseColor("#486FFF"));
                break;
            case 3:
                childViewHolder.child_equip_status.setText("异常");
                gd.setColor(Color.parseColor("#FEAA00"));
                break;
            case 4:
                childViewHolder.child_equip_status.setText("掉线");
                gd.setColor(Color.parseColor("#D2D2D2"));
                break;
        }

        childViewHolder.child_more_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBubbleDialog == null) {
                    mBubbleDialog =  new CustomDevMoreDialog(context);
                }
                mBubbleDialog.setClickedView(view);
                mBubbleDialog.show();
                mBubbleDialog.setIsParent(false);
                mBubbleDialog.setClickListener(new CustomDevMoreDialog.OnClickCustomButtonListener() {
                    @Override
                    public void onClick(String str, int index) {
                        if(onDevicesOperationListener != null){
                            onDevicesOperationListener.onItemClicked(false,index,groupPosition,childPosition,gridViewBeanList.get(groupPosition).getId());
                        }
                        mBubbleDialog.dismiss();
                    }
                });

            }
        });

        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


    public void setGridViewBeanList(List<DeviceBean> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        mSelectedPositions = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    /**
     * 全选
     * */
    public void sellectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }
    /**
     * 取消全选
     * */
    public void cancelSelectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
    //获得选中条目的结果  id的集合
    public ArrayList<DeviceBean> getSelectedItem() {
        ArrayList<DeviceBean> selectList = new ArrayList<>();
        for (int i = 0; i < gridViewBeanList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(gridViewBeanList.get(i));
            }
        }
        return selectList;
    }

    /**
     *  控制多选是否显示
     */
    public void setCheckOnOff( boolean onOff) {
        this.isShow = onOff;
        list.clear();
        notifyDataSetChanged();
    }

    /**
     * isParent 是否是主设备
     * bubblePosition 当前气泡index
     * groupIndex 父 index
     * */
    public interface OnDevicesOperationListener {
        void onItemClicked(boolean isParent, int bubblePosition,int groupIndex,int childPosition,String groupId);
    }


    static class GroupViewHolder {
        @BindView(R.id.tab_tag)
        TextView tab_tag;
        @BindView(R.id.main_title)
        TextView main_title;
        @BindView(R.id.sub_title)
        TextView sub_title;
        @BindView(R.id.group_title)
        TextView group_title;
        @BindView(R.id.equip_status)
        TextView equip_status;
        @BindView(R.id.ststus_circle)
        TextView ststus_circle;
        @BindView(R.id.dev_rl)
        RelativeLayout dev_rl;
        @BindView(R.id.checkbox_rl)
        RelativeLayout checkbox_rl;
        @BindView(R.id.cell_content)
        RelativeLayout cell_content;
        @BindView(R.id.sel_check)
        TextView sel_check;
        @BindView(R.id.more_img)
        ImageView more_img;
        @BindView(R.id.expand_img)
        ImageView expand_img;
        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @BindView(R.id.main_title)
        TextView main_title;
        @BindView(R.id.sub_title)
        TextView sub_title;
        @BindView(R.id.position_img)
        ImageView position_img;
        @BindView(R.id.equip_status)
        TextView child_equip_status;
        @BindView(R.id.ststus_circle)
        TextView child_ststus_circle;
        @BindView(R.id.more_img)
        ImageView child_more_img;
        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }



}
